<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

/**
 * Class JpushShMessage
 *
 * @package App
 * @mixin \Eloquent
 */
class JpushShMessage extends Model
{
    protected $table = "jpush_sh_message";
    public $timestamps = false;


    const TYPE_SYSTEM = 1;
    const TYPE_ORDER_RECEIVE = 2;
    const TYPE_COMLPAINT = 3;
    const TYPE_ORDER_DONE = 4;
    const TYPE_WITHDRAW_DONE = 5;

    const RECEIVE_SH = 1;
    const RECEIVE_DOC = 2;

    /**
     * 发送系统消息
     * @param $receive_id
     * @param $type
     * @param $content
     * @param $is_doc_sh
     * @return array|\Exception
     * @throws \Exception
     */
    static function SendJpush($receive_id, $type, $content, $is_doc_sh)
    {
        DB::beginTransaction();

        //添加记录
        $log = new  JpushShMessage();
        $log->receive_id = $receive_id;
        $log->type = $type;
        $log->content = $content;
        $log->is_doc_sh = $is_doc_sh;
        $log->create_time = date('Y-m-d H:i:s');

        if (!$log->save()) {
            DB::rollBack();

            return error('系统消息发送失败');
        }

        //发送推送
        if ($is_doc_sh === self::RECEIVE_SH) {
            $alais = 'sh'.$receive_id;
        } else {
            $alais = 'doc'.$receive_id;
        }

        $Jpush = Curl::PushOne($alais, $content,$type);
        if ($Jpush['status'] === 2) {
            DB::rollBack();

            return $Jpush;
        }

        DB::commit();

        return success();
    }
}
